﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelTwoButton : MonoBehaviour
{
    public Button topButton;
    public Button bottomButton;
    public List<Text> topTextList;
    public List<Text> bottomTextList;
    public Transform topListRoot;
    public Transform bottomListRoot;
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        topButton.onClick.AddListener(() =>
        {
            if (GameCtrl.Instance.curGameState == GameState.Play)
                TopButtonClick();
        });

        bottomButton.onClick.AddListener(() =>
        {
            if (GameCtrl.Instance.curGameState == GameState.Play)
                BottomButtonClick();
        });
    }

    void TopButtonClick()
    {
        SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
        if (topTextList.Count > 0)
        {
            Text tempText = topTextList[0];
            tempText.transform.SetParent(bottomListRoot);
            tempText.transform.SetAsLastSibling();
            topTextList.RemoveAt(0);
            bottomTextList.Add(tempText);
            JudgeResult();
        }
    }

    void BottomButtonClick()
    {
        SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
        if (bottomTextList.Count > 0)
        {
            Text tempText = bottomTextList[0];
            tempText.transform.SetParent(topListRoot);
            tempText.transform.SetAsLastSibling();
            bottomTextList.RemoveAt(0);
            topTextList.Add(tempText);
            JudgeResult();
        }
    }

    void JudgeResult()
    {
        int count = topTextList.Count;
        if (count == 4)
        {
            string tempStr = string.Empty;
            for (int i = 0; i < count; i++)
            {
                tempStr += topTextList[i].text;
            }
            if (tempStr.Equals(UConst.Str_TargetLove))
                gameLevelItem.TriggerResult(true);
        }
        else
        {
            count = bottomTextList.Count;
            if (count == 4)
            {
                string tempStr = string.Empty;
                for (int i = 0; i < count; i++)
                {
                    tempStr += bottomTextList[i].text;
                }
                if (tempStr.Equals(UConst.Str_TargetLove))
                    gameLevelItem.TriggerResult(true);
            }
        }

    }
}
